package com.zfl.controller;

import com.zfl.po.UserInfo;
import com.zfl.service.UserService;
import com.zfl.util.RedisUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("init")
    public void init(HttpSession httpSession) {
        redisUtil.saveObjectByByte("s001",1);
        redisUtil.saveObjectByByte("s002",2);
        redisUtil.saveObjectByByte("/user/listall",2);
        redisUtil.saveObjectByByte("/user/list",1);

    }

    @GetMapping("login")
    public void login(HttpSession httpSession,String userId) {
        httpSession.setAttribute("userId",userId);


    }

    @GetMapping("listall")
    public List<UserInfo> getAllUserInfo(HttpServletRequest request) {
        String url  = request.getRequestURI().toString();
        System.out.println("url = " + url);
        return userService.getAllUserInfo();
    }

    @GetMapping("list")
    public UserInfo getUserInfoByUserId(HttpServletRequest request,String userId) {
        String url  = request.getRequestURL().toString();
        userId = "s001";
        System.out.println("url = " + url);
        return userService.getUserInfoByUserId(userId);
    }


}
